blob: 979e99c0b27cc39af3f3a050df16a70b8f1221e8 [file] [log] [blame]
Harald Alvestrandfb648512017-01-03 16:49:161<!doctype html>
2<!--
3This test uses data only, and thus does not require fake media devices.
4-->
5
6<html>
7<head>
8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
9 <title>RTCPeerConnection GetStats</title>
10</head>
11<body>
12 <div id="log"></div>
13 <h2>Retrieved stats info</h2>
14 <pre>
Youenn Fableta6be85c2018-11-13 22:25:1915 <input type="button" onclick="showStats()" value="Show stats"></input>
Harald Alvestrandfb648512017-01-03 16:49:1616 <div id="stats">
17 </div>
18 </pre>
19
20 <!-- These files are in place when executing on W3C. -->
21 <script src="/resources/testharness.js"></script>
22 <script src="/resources/testharnessreport.js"></script>
23 <script type="text/javascript">
24 var test = async_test('Can get stats from a basic WebRTC call.');
Youenn Fableta6be85c2018-11-13 22:25:1925 var statsToShow;
Harald Alvestrandfb648512017-01-03 16:49:1626 var gFirstConnection = null;
27 var gSecondConnection = null;
28
29 var onIceCandidateToFirst = test.step_func(function(event) {
30 // If event.candidate is null = no more candidates.
31 if (event.candidate) {
32 gSecondConnection.addIceCandidate(event.candidate);
33 }
34 });
35
36 var onIceCandidateToSecond = test.step_func(function(event) {
37 if (event.candidate) {
38 gFirstConnection.addIceCandidate(event.candidate);
39 }
40 });
41
Harald Alvestrandfb648512017-01-03 16:49:1642 var getStatsRecordByType = function(stats, type) {
43 for (let stat of stats.values()) {
44 if (stat.type == type) {
45 return stat;
46 }
47 }
48 return null;
49 }
50
Harald Alvestrandef4de3d2017-01-04 09:49:2151 var onIceConnectionStateChange = test.step_func(function(event) {
52 // Wait until connection is established.
53 // Note - not all browsers reach 'completed' state, so we're
54 // checking for 'connected' state instead.
55 if (gFirstConnection.iceConnectionState != 'connected') {
56 return;
57 }
58 gFirstConnection.getStats()
59 .then(function(report) {
Harald Alvestrandef4de3d2017-01-04 09:49:2160 let reportDictionary = {};
61 for (let stats of report.values()) {
62 reportDictionary[stats.id] = stats;
63 }
Youenn Fableta6be85c2018-11-13 22:25:1964 statsToShow = JSON.stringify(reportDictionary, null, 2);
Harald Alvestrandef4de3d2017-01-04 09:49:2165 // Check the stats properties.
Harald Alvestrand7d504a22017-10-17 22:21:5566 assert_not_equals(report, null, 'No report');
Harald Alvestrandef4de3d2017-01-04 09:49:2167 let sessionStat = getStatsRecordByType(report, 'peer-connection');
68 assert_not_equals(sessionStat, null, 'Did not find peer-connection stats');
jugglinmikeefa1c1e2018-09-24 09:49:5869 assert_own_property(sessionStat, 'dataChannelsOpened', 'no dataChannelsOpened stat');
Harald Alvestrand7d504a22017-10-17 22:21:5570 // Once every 4000 or so tests, the datachannel won't be opened when the getStats
71 // function is done, so allow both 0 and 1 datachannels.
72 assert_true(sessionStat.dataChannelsOpened == 1 || sessionStat.dataChannelsOpened == 0,
73 'dataChannelsOpened count wrong');
Harald Alvestrandef4de3d2017-01-04 09:49:2174 test.done();
75 })
76 .catch(test.step_func(function(e) {
77 assert_unreached(e.name + ': ' + e.message + ': ');
78 }));
79 });
80
Harald Alvestrandfb648512017-01-03 16:49:1681 // This function starts the test.
82 test.step(function() {
83 gFirstConnection = new RTCPeerConnection(null);
84 gFirstConnection.onicecandidate = onIceCandidateToFirst;
Harald Alvestrandef4de3d2017-01-04 09:49:2185 gFirstConnection.oniceconnectionstatechange = onIceConnectionStateChange;
Harald Alvestrandfb648512017-01-03 16:49:1686
87 gSecondConnection = new RTCPeerConnection(null);
88 gSecondConnection.onicecandidate = onIceCandidateToSecond;
Harald Alvestrandfb648512017-01-03 16:49:1689
90 // The createDataChannel is necessary and sufficient to make
91 // sure the ICE connection be attempted.
92 gFirstConnection.createDataChannel('channel');
Harald Alvestrandfb648512017-01-03 16:49:1693 var atStep = 'Create offer';
94
95 gFirstConnection.createOffer()
96 .then(function(offer) {
97 atStep = 'Set local description at first';
98 return gFirstConnection.setLocalDescription(offer);
99 })
100 .then(function() {
101 atStep = 'Set remote description at second';
102 return gSecondConnection.setRemoteDescription(
103 gFirstConnection.localDescription);
104 })
105 .then(function() {
106 atStep = 'Create answer';
107 return gSecondConnection.createAnswer();
108 })
109 .then(function(answer) {
110 atStep = 'Set local description at second';
111 return gSecondConnection.setLocalDescription(answer);
112 })
113 .then(function() {
114 atStep = 'Set remote description at first';
115 return gFirstConnection.setRemoteDescription(
116 gSecondConnection.localDescription);
117 })
Harald Alvestrandfb648512017-01-03 16:49:16118 .catch(test.step_func(function(e) {
119 assert_unreached('Error ' + e.name + ': ' + e.message +
120 ' happened at step ' + atStep);
121 }));
122 });
Youenn Fableta6be85c2018-11-13 22:25:19123
124 function showStats() {
125 // Show the retrieved stats info
126 var showStats = document.getElementById('stats');
127 showStats.innerHTML = statsToShow;
128 }
129
Harald Alvestrandfb648512017-01-03 16:49:16130</script>
131
132</body>
133</html>